import { Context, Next } from 'koa'
import {
  IConfigPermissionsParams,
  IGetMenuByRoleParams,
  IRoleCreatParams,
  IRoleDelParams,
  IRoleEditParams } from '@/types/roles.types'
import roleService from '../service/roles.service'
import { IPagingQueryParams } from '@/types/index.type'
import { DEFAULE_ERROR, ID_PARAMS_IS_REQUIRED } from '@/constants/error-types'

class RolesController {
  /**
   * 新增角色
   * @param ctx
   * @param next
   * @returns
   */
  public async create(ctx: Context, next: Next) {
    const role = ctx.request.body as unknown as IRoleCreatParams
    try {
      const res: any = await roleService.create(role)
      if (res.length) {
        ctx.body = {
          code: 200,
          success: true,
        }
        return
      }
      ctx.body = {
        code: 80001,
        msg: '系统错误',
      }
    } catch (e) {
      console.log(e)
      ctx.body = {
        code: 80001,
        msg: e || '系统异常',
      }
    }
  }

  /**
   * 修改角色
   */
  public async editRole(ctx: Context, next: Next) {
    const role = ctx.request.body as unknown as IRoleEditParams

    try {
      const res = await roleService.edit(role)

      if (res.length) {
        return ctx.app.emit('success', ctx)
      }
      return ctx.app.emit('error', ctx)
    } catch (e) {
      return ctx.app.emit('error', ctx, e)
    }
  }

  /**
   * 获取角色列表
   */
  public async getList(ctx: Context, next: Next) {
    const { pageNum, pageSize, remark, roleName } = ctx.request.query as unknown as IPagingQueryParams
    type res = {
      count: number,
      list: any[]
    }
    let res: res = {
      count: 0,
      list: []
    }
    try {
      const [rows]: any = await roleService.getList({ pageNum, pageSize, remark, roleName })
      const [[count]]: any = await roleService.getCount()
      if (count.count >=0 && rows.length >= 0) {
        res.count = count.count
        res.list = rows
        return ctx.app.emit('success', ctx, res)
      }
      const error = new Error(DEFAULE_ERROR)
      return ctx.app.emit('error', ctx, error)
    } catch (e) {
      console.log(e)

      const error = new Error(DEFAULE_ERROR)
      return ctx.app.emit('error', ctx, error)
    }
  }


  /**
   * 删除角色
   */
  public async delRole(ctx: Context, next: Next) {
    const { id } = ctx.request.query as unknown as IRoleDelParams
    if (!id) {
      const error = new Error(ID_PARAMS_IS_REQUIRED)
      return ctx.app.emit('error', ctx, error)
    }

    try {
      const res = await roleService.delete(id)
      if (res.length) {
        return ctx.app.emit('success', ctx)
      }
      return ctx.app.emit('error', ctx)
    } catch (e) {
      const u = e as Error
      ctx.app.emit('error', ctx, u.message || '系统错误')
    }
  }

  /**
   * 获取菜单列表
   */
  public async getMenuByRole(ctx: Context, next: Next) {
    const { roleId } = ctx.request.query as unknown as IGetMenuByRoleParams
    try {
      const [rows]: any[] = await roleService.getMenuByRole(roleId)
      if (rows.length >= 0) {
        return ctx.app.emit('success', ctx, rows)
      }
      return ctx.app.emit('error', ctx)
    }catch (e) {
      const u = e as Error
      ctx.app.emit('error', ctx, u.message || '系统错误')
    }
  }

  /**
   * 配置权限
   * @param ctx
   * @param next
   * @returns
   */
  public async configPermissions(ctx: Context, next: Next) {
    const { roleId, permissions } = ctx.request.body as unknown as IConfigPermissionsParams
    try {
      const isDel = await roleService.delMenuByRole(String(roleId))
      if (isDel.length <= 0) {
        return ctx.app.emit('error')
      }
      const isConfig = await roleService.configPermissions(roleId, permissions)
      console.log('isConfig', isConfig)
      if (isConfig.length <= 0) {
        return ctx.app.emit('error')
      }
      return ctx.app.emit('success', ctx)
    }catch (e) {
      console.log(e)
      const u = e as Error
      ctx.app.emit('error', ctx, u.message || '系统错误')
    }
  }
}
export default new RolesController()
